package com.haier.nengyuan.myapplication;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.Timer;

public class EnergySceneActivity extends BaseActivity implements View.OnClickListener {
    private ImageView mTitleLeft;
    private TextView mTitleCenter;
    private TextView mTitleRight;

    private ImageView mImgvEarning;
    private TextView mTvEarning1;
    private LinearLayout mLlEnergyCharge;
    private TextView energyPower;
    private float currHourEnergy;
    private float currDayEnergy;
    private float monTal;
    private TextView shouYi;
    private TextView leijifadianliang;
    private TextView dianfei;
    private String mStrMonth;
    private String jingShouStr = "";

    @Override
    public void addFragment(int des, BaseFragment fragment) {
        super.addFragment(des, fragment);
    }

    private LinearLayout mLlEarning;
    private TextView mTvStatisticsTime;
    private LinearLayout mLlEnergyTotal;
    private TextView mTvEarning;
    private Timer mTimer;
    private float num = 0;
    DecimalFormat fnum = new DecimalFormat("##0.00");
    private final int MSGWHAT = 1;
    private String mYMD;
    private float number = 0;


    long time = System.currentTimeMillis();

    private Handler mHandler = new Handler() { //导os包
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);


            switch (msg.what) {
                case MSGWHAT:
                    if (num == 0) {
                        number = msg.arg1;
                        num = 1;
                    }
                    number = number + 4.31f;
                    mTvEarning.setText(fnum.format(number));
                    if (number >= jingShou) {
                        mTvEarning.setText(jingShouStr);
                        mHandler.removeCallbacksAndMessages(null);
                        return;
                    } else {
                        mHandler.sendEmptyMessageDelayed(MSGWHAT, 25);
                    }
                    break;

                default:

                    break;

            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();

        caculerPower();

        if (!(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)) {
            //设置强制竖屏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        //保持屏幕常量
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    /*计算功率*/
    private void caculerPower() {
        String changeHM = MyDateUtil.changeHM(System.currentTimeMillis());
        if (energyPower != null) {
            energyPower.setText("");
            for (int i = 0; i < energyPowers.length; i++) {
                if (energyPowers[i][0].equals(changeHM)) {
                /*时间相同*/
                    energyPower.setText(Math.round(Float.valueOf(energyPowers[i][1])) / 1000f + "");
                } else {
                /*时间不同*/
                    if (energyPowers[i][0].substring(0, 3).equals(changeHM.substring(0, 3))) {
                    /*小时相同*/
                        if (energyPowers[i][0].substring(3, 4).equals(changeHM.substring(3, 4))) {
                        /*分钟的十位相同*/
                            energyPower.setText(Math.round(Float.valueOf(energyPowers[i][1])) / 1000f + "");
                        }
                    } else {
                        continue;
                    }
                }

            }
            if ("".equals(energyPower.getText())) {
                energyPower.setText(0 + "");
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //设置强制竖屏取消,还原屏幕方向
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

        //保持屏幕常量取消
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_energy_scene);
        judgeShouYi();
        initViews();
        initDatas();

//        Log.e("---", "onCreate: ====" + this.getCurrentFocus().toString());
    }

    private void initViews() {
        shouYi = (TextView) findViewById(R.id.shouyi);
        dianfei = (TextView) findViewById(R.id.dianfei);
        leijifadianliang = (TextView) findViewById(R.id.leijifadianliang);
        energyPower = (TextView) findViewById(R.id.energy_power);
        mImgvEarning = (ImageView) findViewById(R.id.imgv_earning);
        mLlEnergyCharge = (LinearLayout) findViewById(R.id.ll_energy_charge);
        mLlEarning = (LinearLayout) findViewById(R.id.ll_earning);
        mLlEarning.setFocusable(true);
        mLlEarning.requestFocus();
        mTvStatisticsTime = (TextView) findViewById(R.id.tv_statistics_time);
        mLlEnergyTotal = (LinearLayout) findViewById(R.id.ll_energy_total);
        mTvEarning = (TextView) findViewById(R.id.tv_earning);
        mTitleLeft = (ImageView) findViewById(R.id.title_left);
        mTitleLeft.setVisibility(View.INVISIBLE);
        mTitleCenter = (TextView) findViewById(R.id.title_center);
        mTitleRight = (TextView) findViewById(R.id.title_right);
        mTvEarning1 = (TextView) findViewById(R.id.tv_earning1);
        mTitleRight.setFocusable(true);
        mTvEarning1.setOnClickListener(this);
        mLlEnergyTotal.setOnClickListener(this);
        mImgvEarning.setOnClickListener(this);
        mLlEnergyCharge.setOnClickListener(this);
        mLlEarning.setOnClickListener(this);

        mTitleLeft.setOnClickListener(this);
        mTitleCenter.setOnClickListener(this);
        mTitleRight.setOnClickListener(this);
    }

    private float jingShou = 0;

    private void initDatas() {
        mYMD = MyDateUtil.changeYMD(time);
        mTvStatisticsTime.setText("统计时间：" + mYMD);
        Message mMessag = new Message();


        caculerPower();
        shouYi.setText((float) (Math.round(0.85 * (monTal + currDayEnergy + currHourEnergy) * 100)) / 100 + "");
//        shouYi.setText((((Math.round((currDayEnergy + monTal + currHourEnergy) * 0.85)) * 100) / 100) + "");
        Log.e("---", "initDatas: " + (currDayEnergy + monTal + currHourEnergy));
        leijifadianliang.setText(fnum.format((currDayEnergy + monTal + currHourEnergy)) + "kWh");
        /*电费*/
        if ("09".equals(mStrMonth)) {
            dianfei.setText(EnergyChargeDetailActivity.dianFeiArrys[0]);
            /*净收益*/
            jingShou = Float.valueOf(((float) (Math.round(0.85 * (monTal + currDayEnergy + currHourEnergy) * 100)) / 100) - Float.valueOf(EnergyChargeDetailActivity.dianFeiArrys[0]));

            Log.e("---", "initDatas: " + jingShou);
        } else if ("10".equals(mStrMonth)) {
            dianfei.setText(EnergyChargeDetailActivity.dianFeiArrys[1]);
            jingShou = Float.valueOf(((float) (Math.round(0.85 * (monTal + currDayEnergy + currHourEnergy) * 100)) / 100) - Float.valueOf(EnergyChargeDetailActivity.dianFeiArrys[1]));


        } else if ("11".equals(mStrMonth)) {
            dianfei.setText(EnergyChargeDetailActivity.dianFeiArrys[2]);
            jingShou = Float.valueOf(((float) (Math.round(0.85 * (monTal + currDayEnergy + currHourEnergy) * 100)) / 100) - Float.valueOf(EnergyChargeDetailActivity.dianFeiArrys[2]));
        } else if ("12".equals(mStrMonth) || "01".equals(mStrMonth)) {
            dianfei.setText(EnergyChargeDetailActivity.dianFeiArrys[2]);//电费
            jingShou = Float.valueOf(((float) (Math.round(0.85 * (monTal + currDayEnergy + currHourEnergy) * 100)) / 100) - Float.valueOf(EnergyChargeDetailActivity.dianFeiArrys[2]));
        }
        jingShouStr = fnum.format(jingShou);

        mMessag.arg1 = (int) (jingShou - 300);
        mMessag.what = MSGWHAT;
        mHandler.sendMessageDelayed(mMessag, 100);
    }


    public void judgeShouYi() {
        mStrMonth = MyDateUtil.changeM(System.currentTimeMillis());
        String mStrDay = MyDateUtil.changeD(System.currentTimeMillis());
        String mStrHour = MyDateUtil.changeH(System.currentTimeMillis());
        int mIntHour1 = (int) (Integer.parseInt(mStrHour) / 2);
        if (mStrMonth.equals("09")) {
            WindowManager wm = getWindowManager();
            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
            for (int i = 0; i < Integer.valueOf(mStrMonth); i++) {
                if (i == Integer.valueOf(mStrMonth) - 1) {
                    /*当前月*/
                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
                        if (k == Integer.valueOf(mStrDay) - 1) {
                            /*当前天*/
                            for (int j = 0; j <= mIntHour1; j++) {
                                /*时间段计算*/
//                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (points4[i][1] / 31.97))));
                                currHourEnergy += (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (BaseFragment.points4[i][1] / 31.97));

                            }
                        } else {
                            /*非当前天*/
                            currDayEnergy += Float.valueOf(Variable.nineDayEnergy[k]);

                        }
//                        energyTotal += Float.valueOf(Variable.nineDayEnergy[k]);

                    }
                } else {
                    /*非当前月*/
                    monTal += Variable.nineMonthEnergy[i];

                }
            }
        }
        if (mStrMonth.equals("10")) {
            WindowManager wm = getWindowManager();
            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
            for (int i = 0; i < Integer.valueOf(mStrMonth); i++) {
                if (i == Integer.valueOf(mStrMonth) - 1) {
                    /*当前月*/
                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
                        if (k == Integer.valueOf(mStrDay) - 1) {
                            /*当前天*/
                            for (int j = 0; j <= mIntHour1; j++) {
                                /*时间段计算*/
//                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (points4[i][1] / 31.97))));
                                currHourEnergy += (float) (Float.valueOf(Variable.tenDayEnergy[i]) * (BaseFragment.points4[i][1] / 31.97));

                            }
                        } else {
                            /*非当前天*/
                            currDayEnergy += Float.valueOf(Variable.tenDayEnergy[k]);

                        }
//                        energyTotal += Float.valueOf(Variable.nineDayEnergy[k]);

                    }
                } else {
                    /*非当前月*/
                    monTal += Variable.nineMonthEnergy[i];

                }
            }
        }
        if (mStrMonth.equals("11")) {
            WindowManager wm = getWindowManager();
            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
            for (int i = 0; i < Integer.valueOf(mStrMonth); i++) {
                if (i == Integer.valueOf(mStrMonth) - 1) {
                    /*当前月*/
                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
                        if (k == Integer.valueOf(mStrDay) - 1) {
                            /*当前天*/
                            for (int j = 0; j <= mIntHour1; j++) {
                                /*时间段计算*/
//                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (points4[i][1] / 31.97))));
                                currHourEnergy += (float) (Float.valueOf(Variable.eleEnergy[i]) * (BaseFragment.points4[i][1] / 31.97));

                            }
                        } else {
                            /*非当前天*/
                            currDayEnergy += Float.valueOf(Variable.eleEnergy[k]);

                        }
//                        energyTotal += Float.valueOf(Variable.nineDayEnergy[k]);

                    }
                } else {
                    /*非当前月*/
                    monTal += Variable.nineMonthEnergy[i];

                }
            }
        }
        if (mStrMonth.equals("12")) {
            WindowManager wm = getWindowManager();
            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
            for (int i = 0; i < 12; i++) {
                if (i == 12 - 1) {
                    /*当前月*/
                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
                        if (k == Integer.valueOf(mStrDay) - 1) {
                            /*当前天*/
                            for (int j = 0; j <= mIntHour1; j++) {
                                /*时间段计算*/
//                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (points4[i][1] / 31.97))));
                                currHourEnergy += (float) (Variable.decemberEnergy[i] * (BaseFragment.points4[i][1] / 31.97));

                            }
                        } else {
                            /*非当前天*/
                            currDayEnergy += Variable.decemberEnergy[k];

                        }
//                        energyTotal += Float.valueOf(Variable.nineDayEnergy[k]);

                    }
                } else {
                    /*非当前月*/
                    monTal += Variable.nineMonthEnergy[i];

                }
            }
        }
        if (mStrMonth.equals("01")) {
            WindowManager wm = getWindowManager();
            int width = (wm.getDefaultDisplay().getWidth() - 120) / 13;
            for (int i = 0; i < 13; i++) {
                if (i == 13 - 1) {
                    /*当前月*/
                    for (int k = 0; k < Integer.valueOf(mStrDay); k++) {
                        if (k == Integer.valueOf(mStrDay) - 1) {
                            /*当前天*/
                            for (int j = 0; j <= mIntHour1; j++) {
                                /*时间段计算*/
//                                mPointArrayList.add(new PointF(points4[i][0], (float) (Float.valueOf(Variable.nineDayEnergy[i]) * (points4[i][1] / 31.97))));
                                currHourEnergy += (float) (Variable.oneEnergy[i] * (BaseFragment.points4[i][1] / 31.97));

                            }
                        } else {
                            /*非当前天*/
                            currDayEnergy += Variable.oneEnergy[k];

                        }
//                        energyTotal += Float.valueOf(Variable.nineDayEnergy[k]);

                    }
                } else {
                    /*非当前月*/
                    monTal += Variable.nineMonthEnergy[i];

                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_energy_total) {

            //累计发电量
            Intent intent = new Intent(EnergySceneActivity.this, EnergyTotalActivity.class);
            EnergySceneActivity.this.startActivity(intent);
        }
        if (id == R.id.imgv_earning) {
//            //净收益提示
//            Intent intent = new Intent(EnergySceneActivity.this, EarningActivity.class);
//            EnergySceneActivity.this.startActivity(intent);

        }
        if (id == R.id.tv_earning1) {
            //净收益提示
//            Intent intent = new Intent(EnergySceneActivity.this, EarningActivity.class);
//            EnergySceneActivity.this.startActivity(intent);
            View inflate = LayoutInflater.from(EnergySceneActivity.this).inflate(R.layout.activity_earning, null);
            ImageView close = (ImageView) inflate.findViewById(R.id.close);

            final PopupWindow popupWindow = new PopupWindow(inflate, (int) (getWindowManager().getDefaultDisplay().getWidth() / 1.3), (int) (getWindowManager().getDefaultDisplay().getHeight() / 1.8));
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable());
            setWindowBackGround(0.5f);
            close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAtLocation(findViewById(R.id.contain), Gravity.CENTER, 0, 0);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    setWindowBackGround(1f);
                }
            });

        }
        if (id == R.id.ll_energy_charge) {
            //电费
            Intent intent = new Intent(EnergySceneActivity.this, EnergyChargeDetailActivity.class);
            EnergySceneActivity.this.startActivity(intent);
        }
        if (id == R.id.ll_earning) {
            //收益
            Intent intent = new Intent(EnergySceneActivity.this, EarningDetailActivity01.class);
            EnergySceneActivity.this.startActivity(intent);
        }
        if (id == R.id.title_left) {
            finish();
        }
        if (id == R.id.title_center) {
            mTitleCenter.setText("能源中心");
        }
        if (id == R.id.title_right) {
            //缴费
            mTitleRight.setText("缴费");
            Intent intent = new Intent(EnergySceneActivity.this, PayElectricityBillActivity.class);
            EnergySceneActivity.this.startActivity(intent);
        }
    }

    public void setWindowBackGround(float backGround) {
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = backGround;
        getWindow().setAttributes(attributes);
    }

}
